{{/* Check parameter types */ -}}
{{ with .Get "langEqualsHeader" -}}
  {{ if ne ( printf "%T" . ) "bool" -}}
    {{ errorf "shortcode tabpane: parameter 'langEqualsHeader' must be either true or false" -}}
  {{ end -}}
{{ end -}}

{{ with .Get "text" -}}
  {{ if ne ( printf "%T" . ) "bool" -}}
    {{ errorf "shortcode tabpane: parameter 'text' must be either true or false" -}}
  {{ end -}}
{{ end -}}

{{ with .Get "persistLang" -}}
  {{ if ne ( printf "%T" . ) "bool" -}}
    {{ errorf "shortcode tabpane: parameter 'persistLang' must be either true or false" -}}
  {{ end -}}
{{ end -}}

{{ with .Get "right" -}}
  {{ if ne ( printf "%T" . ) "bool" -}}
    {{ errorf "shortcode tabpane: parameter 'right' must be either true or false" -}}
  {{ end -}}
{{ end -}}

{{/* Set values given defined within tabpane */ -}}
{{ $langPane := default "" ($.Get "lang") -}}
{{ $hloptionsPane := default "" ($.Get "highlight") -}}
{{ $textPane := default false ($.Get "text") -}}
{{ $langEqualsHeader := default false ($.Get "langEqualsHeader") -}}
{{ $persistLang := default true ($.Get "persistLang") -}}
{{ $rightPane := default false ($.Get "right") -}}
{{ $activeSet := false -}}
{{- /* Scratchpad gets populated through call to .Inner */ -}}
{{- .Inner -}}

{{ $langs := slice -}}
{{ $duplicate := false -}}
{{ $duplicateLang := "" -}}

{{ $Ordinal := $.Ordinal -}}
{{ if ge hugo.Version "0.93.0" -}}
  {{ if .Page.Store.Get "Ordinal" -}}
    {{ $Ordinal = .Page.Store.Get "Ordinal" -}}
  {{end -}}
{{ end -}}

{{/* Nav tabs */ -}}
<ul class="nav nav-tabs{{ if $rightPane }} justify-content-end{{ end }}" id="tabs-{{- $Ordinal -}}" role="tablist">
  {{ range $index, $element := $.Scratch.Get "tabs" -}}

    {{ $disabled := false -}}
    {{ with $element.disabled -}}
      {{ $disabled = . -}}
    {{ end -}}

    {{ $lang := $langPane -}}
    {{ if $langEqualsHeader -}}
      {{ $lang = $element.header -}}
    {{ end -}}
    {{ with $element.language -}}
      {{ $lang = $element.language -}}
    {{ end -}}

    {{/* Check for duplicate languages */ -}}
    {{ if and $persistLang (not $duplicate) -}}
      {{ if and (not $disabled) (ne $lang "") -}}
        {{ $langs = $langs | append $lang -}}
      {{ end -}}
      {{ if ne $langs (uniq $langs) -}}
        {{ $duplicate = true -}}
        {{ $duplicateLang = $lang -}}
      {{ end -}}
    {{ end -}}

    {{ $rightpush := false -}}
    {{ with $element.rightpush -}}
      {{ $rightpush = . -}}
    {{ end -}}

    {{/* Replace space and +, not valid for css selectors */ -}}
    {{ $lang := replaceRE "[\\s+]" "-" $lang -}}
    <li class="nav-item{{ if $rightpush }} ms-auto{{ end -}}">
      {{/* Generate the IDs for the <a> and the <div> elements */ -}}
      {{ $tabid := printf "tabs-%02v-%v-tab" $Ordinal $index | anchorize -}}
      {{ $entryid := printf "tabs-%02v-%v" $Ordinal $index | anchorize -}}

      <button class="nav-link{{ if and ( not $activeSet ) ( not $disabled ) }} active{{ end }}{{ if $disabled }} disabled{{ end }}{{ if ne $lang "" }}{{ if and $persistLang (not $duplicate) }} persistLang-{{- $lang -}}{{ end }}{{ end }}"
          id="{{ $tabid }}" data-bs-toggle="tab" data-bs-target="#{{ $entryid }}" role="tab"
          {{ if ne $lang "" }}{{ if and $persistLang (not $duplicate) }}onclick="persistLang({{ $lang }});"{{ end }}{{ end -}}
          aria-controls="{{- $entryid -}}" aria-selected="{{- cond ( and ( not $activeSet ) ( not $disabled ) ) "true" "false" -}}">
        {{ index . "header" | markdownify }}
      </button>
    </li>

    {{- if not $disabled -}}
      {{ $activeSet = true -}}
    {{ end -}}

  {{- end }}
</ul>

{{ if $duplicate -}}
  {{ warnf "Tabpane on page '%s': duplicate language '%s' detected, disabling persistance of language to avoid multiple tab display." .Page.Title $duplicateLang  -}}
{{ end -}}
{{ $duplicate = false -}}
{{ $langs = slice -}}
{{ $activeSet = false -}}

{{/* Tab panes */ -}}
<div class="tab-content" id="tabs-{{ $Ordinal }}-content">
  {{- range $index, $element := $.Scratch.Get "tabs" -}}

    {{ $lang := $langPane -}}
    {{ if $langEqualsHeader -}}
      {{ $lang = $element.header -}}
    {{ end -}}
    {{ with $element.language -}}
      {{ $lang = $element.language -}}
    {{ end -}}

    {{ $disabled := false -}}
    {{ with $element.disabled -}}
      {{ $disabled = . -}}
    {{ end -}}

    {{ $hloptions := $hloptionsPane -}}
    {{ with $element.highlight -}}
      {{ $hloptions = $element.highlight -}}
    {{ end -}}

    {{ $text := $textPane -}}
    {{ with $element.text -}}
      {{ $text = . }}
    {{ end -}}

    {{ $tabid := printf "tabs-%02v-%v-tab" $Ordinal $index | anchorize -}}
    {{ $entryid := printf "tabs-%02v-%v" $Ordinal $index | anchorize }}
    <div class="{{ if $text }}tab-body {{end}}tab-pane fade{{ if and ( not $activeSet ) ( not $disabled ) }} show active{{ end }}"
        id="{{ $entryid }}" role="tabpanel" aria-labelled-by="{{ $tabid }}" tabindex="{{ $Ordinal }}">
        {{ if $text -}}
          {{ index . "content" -}}
        {{ else -}}
          {{ highlight (trim (index . "content") "\n") $lang $hloptions -}}
        {{ end }}
    </div>

    {{- if not $disabled -}}
      {{ $activeSet = true -}}
    {{ end -}}

  {{- end }}
</div>
